package cn.shop.dys.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import java.util.List;

import cn.shop.dys.bean.SearchHistoryInfo;
import cn.shop.dys.util.SearchHistoryInfoDBUtil;
import cn.shop.lib_common.CommonViewHolder;
import cn.shop.dys.R;

/**
 * 搜索历史适配器
 * Created by sunjian on 2016/4/20.
 */
public class SearchHistoryAdapter extends BaseAdapter{
    private Context mContext;
    private List<SearchHistoryInfo> mSearchHistoryInfos;

    public SearchHistoryAdapter(List<SearchHistoryInfo> mSearchHistoryInfos, Context mContext) {
        this.mSearchHistoryInfos = mSearchHistoryInfos;
        this.mContext = mContext;
    }

    private View.OnClickListener mListener=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Integer position= (Integer) v.getTag();
            SearchHistoryInfoDBUtil.deleteSearchHistoryInfoBySearchContent(mSearchHistoryInfos.get(position).getSearch_content());
            mSearchHistoryInfos.remove(mSearchHistoryInfos.get(position));
            notifyDataSetChanged();
        }
    };

    @Override
    public int getCount() {
        return mSearchHistoryInfos==null?0:mSearchHistoryInfos.size();
    }

    @Override
    public SearchHistoryInfo getItem(int position) {
        return mSearchHistoryInfos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        CommonViewHolder holder = CommonViewHolder.getCommonViewHolder(convertView, mContext, R.layout.search_item_history_list);
        SearchHistoryInfo searchHistoryInfo = mSearchHistoryInfos.get(position);
        holder.getTv(R.id.tv_goods_name).setText(searchHistoryInfo.getSearch_content());
        ImageView goodsHistoryDelete = holder.getIv(R.id.iv_goods_history_delete);
        goodsHistoryDelete.setTag(position);
        goodsHistoryDelete.setOnClickListener(mListener);
        return holder.convertView;
    }
}
